package com.ztsoft.user.utils;

import com.ztsoft.user.pojo.vo.user.DeptTree;
import com.ztsoft.user.pojo.vo.user.SysDeptTree;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门树结构工具
 */
public class DeptTreeUtils {

    private DeptTreeUtils() {
    }

    /**
     * 构建树
     * 1、数据库查出的部门记录装载在承载部门的列表中
     * 2、构建树（获取根节点，遍历根节点，对每一个根节点构建子树）
     * 3、得到最终树形部门
     *
     * @param deptTreeList 需要拼装的树形层级结构数据集合
     * @return 树形层级结构数据集合
     */
    public static List<SysDeptTree> buildDeptTreeSelect(List<SysDeptTree> deptTreeList) {
        List<SysDeptTree> deptTreeParentList = new ArrayList<>();
        List<SysDeptTree> deptTreeChildrenList = new ArrayList<>();

        // 获取根节点
        deptTreeList.stream().filter(deptTree -> deptTree.getParentId().equals("0")).forEach(deptTreeParentList::add);
        // 获取子节点（排除掉根节点，可防止根节点中的空值引发的空指针异常）
        deptTreeList.stream().filter(deptTree -> !deptTreeParentList.contains(deptTree)).forEach(deptTreeChildrenList::add);
        // 构建树
        deptTreeParentList.forEach(menuTreeParent -> buildDeptChildren(menuTreeParent, deptTreeChildrenList));
        return deptTreeParentList;
    }

    /**
     * 构建子树
     *
     * @param deptTreeParent       根节点
     * @param deptTreeChildrenList 子节点集合
     * @return 树形层级结构根节点
     */
    public static SysDeptTree buildDeptChildren(SysDeptTree deptTreeParent, List<SysDeptTree> deptTreeChildrenList) {
        List<SysDeptTree> childrenTree = new ArrayList<>();
        deptTreeChildrenList.stream().filter(dept -> dept.getParentId().equals(deptTreeParent.getValue())).forEach(deptTree -> childrenTree.add(buildDeptChildren(deptTree, deptTreeChildrenList)));
        deptTreeParent.setChildren(childrenTree);
        return deptTreeParent;
    }


    public static List<DeptTree> buildDeptAndUserTree(List<DeptTree> deptTreeList) {
        List<DeptTree> deptTreeParentList = new ArrayList<>();
        List<DeptTree> deptTreeChildrenList = new ArrayList<>();

        // 获取根节点
        deptTreeList.stream().filter(deptTree -> deptTree.getParentId().equals("0")).forEach(deptTreeParentList::add);
        // 获取子节点（排除调根节点，可防止根节点中的空值引发的空指针异常）
        deptTreeList.stream().filter(deptTree -> !deptTreeParentList.contains(deptTree)).forEach(deptTreeChildrenList::add);
        // 构建树
        deptTreeParentList.forEach(menuTreeParent -> buildDeptAndUserChildren(menuTreeParent, deptTreeChildrenList));
        return deptTreeParentList;
    }

    /**
     * 构建子树
     *
     * @param deptTreeParent       根节点
     * @param deptTreeChildrenList 子节点集合
     * @return 树形层级结构根节点
     */
    public static DeptTree buildDeptAndUserChildren(DeptTree deptTreeParent, List<DeptTree> deptTreeChildrenList) {
        List<DeptTree> childrenTreeList = new ArrayList<>();

        for (DeptTree deptTree : deptTreeChildrenList) {
            if (deptTree.getParentId().equals(deptTreeParent.getValue())) {
                childrenTreeList.add(buildDeptAndUserChildren(deptTree, deptTreeChildrenList));
            }
        }
        // 如果是树型末尾，添加用户信息
        if (!childrenTreeList.isEmpty()) {
            deptTreeParent.setChildren(childrenTreeList);
        }
        return deptTreeParent;
    }

}